package stormdemo;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Tuple;

public class ReportBolt extends BaseRichBolt {
	private static final long serialVersionUID = -5856165859137409441L;
	Map<String,Long> wcMap = null;
	
	@Override
	public void prepare(@SuppressWarnings("rawtypes") Map stormConf, TopologyContext context, OutputCollector collector) {
		this.wcMap = new HashMap<>();
	}

	@Override
	public void execute(Tuple input) {
		String world = input.getStringByField("world");
		Long count = input.getLongByField("count");
		wcMap.put(world, count);
	}

	@Override
	public void declareOutputFields(OutputFieldsDeclarer declarer) {

	}
	
	@Override
	public void cleanup() {
		System.out.println("--------------cleanup----------------");
		for (Entry<String,Long> entry : wcMap.entrySet()) {
			System.out.println(entry.getKey()+":"+entry.getValue());
		}
		System.out.println("------------------------------");
	}

}
